package br.com.busgps.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.busgps.util.Constants;
import br.com.busgps.util.PropertiesLoad;

public class ConnectionDB
{
    private static Logger logger = LoggerFactory.getLogger(ConnectionDB.class);

    public static Connection createConnection()
    {
        try
        {
            long start = System.currentTimeMillis();

            String driverClassName = PropertiesLoad.getValue(Constants.DATABASE_JDBC_DRIVER_CLASS_NAME);
            String jdbcUrl = PropertiesLoad.getValue(Constants.DATABASE_JDBC_URL);
            String jdbcUserName = PropertiesLoad.getValue(Constants.DATABASE_JDBC_USERNAME);
            String jdbcPassword = PropertiesLoad.getValue(Constants.DATABASE_JDBC_PASSWORD);

            if (driverClassName == null || jdbcUrl == null || jdbcUserName == null || jdbcPassword == null)
            {
                String[] args = { driverClassName, jdbcUrl, jdbcUserName, jdbcPassword };
                logger.error("Erro nos parametros de conexao com o banco. driverClassName[{}], jdbcUrl[{}], jdbcUserName[{}}, jdbcPassword[{}]",args);
                return null;
            }

            Class.forName(driverClassName);

            Connection connection = DriverManager.getConnection(jdbcUrl, jdbcUserName, jdbcPassword);

            logger.info("Tempo para abrir conexao no banco: " + (System.currentTimeMillis() - start));

            return connection;
        }
        catch (Exception e)
        {
            logger.error("Erro na conexao com o banco. " + e);
        }

        return null;
    }

    public static void closeConnection(Connection connection, Statement stmt, ResultSet rs)
    {

        try
        {
            close(connection, stmt, rs);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @param connection
     * @param stmt
     */
    public static void closeConnection(Connection connection, Statement stmt)
    {

        try
        {
            close(connection, stmt, null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @param connection
     */
    public static void closeConnection(Connection connection)
    {

        try
        {
            close(connection, null, null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @param connection
     * @param stmt
     * @param rs
     */
    private static void close(Connection connection, Statement stmt, ResultSet rs)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
            }

            if (stmt != null)
            {
                stmt.close();
            }

            if (connection != null)
            {
                connection.close();
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}